#ifndef QSOFTCORE_H
#define QSOFTCORE_H

#include "core_global.h"

#include "../communicate/qcommunicate.h"

#include <QObject>
#include <QMap>

#define SOFT_VERSION        10000
#define SOFT_VERSION_STRING "1.0.0"

class QMenuActionMap;
class QMdiSubWindow;
class QFunCodeDataBase;
class QMachine;
class QCommunicate;
class QSettingManager;
class QMachineValue;
class QLogManager;
class QParamDialog;
class QUpdateManager;
class QFaultManager;

struct tagTrendValue
{
    QString     m_name;
    int         m_index;
    int         m_scale;
};

struct tagFaultValue
{
    QString     m_str_code;
    int         m_int_code;
    QString     m_name;
    QString     m_text;
    QString     m_reset;
};

struct tagVersionInfo
{
    int         m_version_code;
    QString     m_version_name;
    QString     m_exe_path;
    QString     m_information;
};

class CORELIB_EXPORT QSoftCore : public QObject
{
    Q_OBJECT
protected:
    explicit QSoftCore(QObject *parent = 0);
public:
    ~QSoftCore();

    static QSoftCore*   get_core();
    static void         release_core();

    void    new_machine(const QString &type,const QString &version);
    void    clear_machine();
    void    release_machine();

    QMachine*           get_machine();

    bool                serial_connect(const tagSerialConfig& config);
    void                serial_disconnect();

    QCommunicate        *get_communicate();

    QVariant            get_setting_value(const QString &key);
    void                set_setting_value(const QString &key,const QVariant &value);
    QList<QMission*>    make_mission(const QList<QMachineValue*> values);

    void                update_group(const QString &name);
    void                update_all();

    QLogManager*        log_manager();
    QFaultManager*      fault_manager();

    int                 get_machine_address();
    void                set_machine_address(int address);


    void                emit_mission_group_done(bool success);

    QParamDialog*       get_param_dialog();
    QList<tagTrendValue*>   get_trend_value();
    tagFaultValue*      get_fault_value(int code);

    QUpdateManager      *update_manager();

    tagVersionInfo      get_new_version();
public:
    QMenuActionMap  *   get_menu_action_map();
protected slots:
    void                mission_success(QMission* ms);
    void                mission_failed(QMission* ms,int code);
    void                new_machine_version(int version);

    void                version_finish();
signals:
    void    add_sub_window(QMdiSubWindow *sub_window);
    void    funcode_changed();

    void    new_machine_signal();
    void    clear_machine_signal();
    void    machine_remove_signal();

    void    mission_group_done(bool success);

    void    start_runtime();

    void    new_version();
public slots:
protected:
    QMenuActionMap      *m_menu_action_map;
    QFunCodeDataBase    *m_funcode_database;

    QMachine            *m_machine;
    QSettingManager     *m_setting;
    QCommunicate        *m_communicate;
    QLogManager         *m_log_manager;
    int                 m_machine_address;

    QParamDialog        *m_param_dialog;

    QList<tagTrendValue*>           m_trend_value;
    QMap<int,tagFaultValue*>           m_fault_value;
    QUpdateManager      *m_update_manager;
    tagVersionInfo      m_new_version_info;

    QFaultManager       *m_fault_manager;
protected:
    static  QSoftCore*  m_core;
};


extern void CORELIB_EXPORT append_log(const QString& tag,const QString& text);

#endif // QSOFTCORE_H
